package com.vxb.testmvp.test_zonghe.test_5;

import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.holenzhou.pullrecyclerview.BaseRecyclerAdapter;
import com.holenzhou.pullrecyclerview.BaseViewHolder;
import com.holenzhou.pullrecyclerview.layoutmanager.XLinearLayoutManager;
import com.vxb.testmvp.R;
import com.vxb.testmvp.test_zonghe.entity.ZhuangEntity;
import com.vxb.testmvp.test_zonghe.network.NetworkManager;
import com.vxb.testmvp.test_zonghe.test_1.TestMActivity;
import com.vxb.testmvp.test_zonghe.test_3.ScoreBase;
import com.vxb.testmvp.test_zonghe.test_3.Test3Activity;
import com.vxb.testmvp.test_zonghe.test_3.Test3Adapter;
import com.vxb.testmvp.util.SPUtils;
import com.vxb.testmvp.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class Test5Activity extends AppCompatActivity {


    @Bind(R.id.recycler_view)
    RecyclerView recycler_view;
    @Bind(R.id.tv_error)
    TextView tv_error;
    @Bind(R.id.swipe_refresh)
    SwipeRefreshLayout swipe_refresh;
    private CompositeSubscription mCompositeSubscription;
    private Test5Adapter mAdapter;

    int mPageNum = 1;
    int zongYeShu;
    private List<ScoreBase.ScoreData.ScoreContent.Detail> mDataList = new ArrayList<>();
    private LinearLayoutManager mLinearLayoutManager;
    private List<ScoreBase.ScoreData.ScoreContent.Detail> mData = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test5);
        ButterKnife.bind(this);
        mCompositeSubscription = new CompositeSubscription();
        initView();
        swipe_refresh.setRefreshing(true);
        initData();

    }

    private void initView() {

        mLinearLayoutManager = new LinearLayoutManager(this);
        // 初始化PullRecyclerView
        recycler_view.setLayoutManager(mLinearLayoutManager);

        mAdapter = new Test5Adapter();
        mAdapter.setItems(mDataList);
        recycler_view.setAdapter(mAdapter);


        swipe_refresh.setOnRefreshListener(new MyOnRefreshListener());//:设置手势滑动监听器。
        swipe_refresh.setColorSchemeColors(Color.BLUE, Color.GREEN, Color.RED, Color.YELLOW);//:设置进度圈的背景色。
        // swipe_refresh.setColorSchemeResources(int… colorResIds)//:设置进度动画的颜色。
        // swipe_refresh.setRefreshing(Boolean refreshing)//:设置组件的刷洗状态。


        recycler_view.addOnScrollListener(new EndLessOnScrollListener(mLinearLayoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                    initData();
            }
        });


        mAdapter.setOnItemClickLitener(new Test5Adapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Util.showToast(Test5Activity.this, "positin:" + position);
            }
        });

        tv_error.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    initData();
            }
        });

    }

    class MyOnRefreshListener implements SwipeRefreshLayout.OnRefreshListener {

        @Override
        public void onRefresh() {
            swipe_refresh.setRefreshing(true);



            // clear()后当前的变量和所有引用到这个list的变量都指向同一个空的list，
            // 而如果你new了一个list后再赋值，则当前的变量变为一个空list，
            // 而其他以前引用到初始list的则不受影响，还是之前存有数据的list。
            mDataList.clear();
            initData();
        }
    }


    private void writeData() {

    }

    private List<String> readdData() {
        return null;
    }

    private void initData() {

        tv_error.setVisibility(View.GONE);
        swipe_refresh.setVisibility(View.VISIBLE);

        String token = (String) SPUtils.get("token", "", this);

        mCompositeSubscription.add(NetworkManager.V2Service()
                .getScoreDetail(token, String.valueOf(mPageNum))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Action1<ScoreBase>() {
                            @Override
                            public void call(ScoreBase scoreBase) {

                              mData = scoreBase.getScoreData().getScoreContent().get(0).detailList;

                                zongYeShu = Integer.valueOf(scoreBase.scoreData.totalPage);

                                mDataList.addAll(mData);

                                mAdapter.setItems(mDataList);
                                mAdapter.notifyDataSetChanged();
                                swipe_refresh.setRefreshing(false);
                            }
                        },
                        new Action1<Throwable>() {

                            @Override
                            public void call(Throwable throwable) {
                                throwable.printStackTrace();
                                if (mData.size() != 0) {
                                    Util.showToast(Test5Activity.this, "网络错误");
                                } else {
                                    tv_error.setVisibility(View.VISIBLE);
                                    swipe_refresh.setVisibility(View.GONE);

                                }

                                swipe_refresh.setRefreshing(false);
                            }
                        }));


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }


}
